package com.sky.controller.admin;


import com.sky.result.Result;
import com.sky.utils.AliyunOSSOperator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/admin/common")
@Slf4j
@Api(tags = "通用接口，上传等")
public class CommonController {

    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;


    @ApiOperation("文件上传")
    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file){
        try {
            String originName = file.getOriginalFilename();
            log.info("上传一个文件，原本的命名为：{}", originName);

            String url = aliyunOSSOperator.upload(file.getBytes(), originName);
            log.info("这个就是好宝宝的url----{}", url);
            return Result.success(url);
        } catch (Exception e) {
            log.info("上传失败，出现报错：{}", e.getMessage());
            return Result.error("上传失败" + e.getMessage());
        }
    }

}
